home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / XCMD libraries 960603 / xcmdWind.cp < prev    next >
Text File  |  1996-01-31  |  11KB  |  433 lines

  1. //    © Paul B. Beeken, Work In Progress, 1994-5
  2. //    Knowledge Software Consulting.
  3. //
  4. //    These files are a mindlessly simple wrapper class for the
  5. //    basic XCMD operations.  Only one instance of the xcmdBase class is
  6. //    generated per XCMD call but there may be many instances of the XCMDString
  7. //    class.  I have used these classes to whip out XCMD/XFCNs within hours of
  8. //    receiving the specs.  They work great for me but I will always consider 
  9. //    suggestions.
  10. //
  11. //    Please, please, please, in the unlikely event you should use this stuff
  12. //    for some commercial application I would appreciate you contacting me.  If
  13. //    its for your own use, use away. Send email: knowsoft@ios.com
  14. //
  15. //    As always: this file is presented as is with no warrantees expressed or implied.
  16. //    Swim at your own risk, etc. etc.
  17.  
  18. #include "xcmdWind.h"
  19.  
  20. #pragma mark • Creation and Destruction…
  21.  
  22. xcmdWindow::xcmdWindow( XCmdPtr xP, Boolean lockParameters ) : 
  23.     xcmdBase( xP, lockParameters )     // set up xcmdBase and continue...
  24. {
  25.  
  26.     if ( nParams() < 0 ) {    // This entry involves an "event"
  27.  
  28.         xEventInfo    = XWEventInfoPtr( (*this)[0] );        // Get ext param block
  29.         xEvent        = xEventInfo->event;                // Copy eventRecord
  30.         xWindow        = (CWindowPtr)xEventInfo->eventWindow; // We are always color
  31.         xPrivStore    = (Handle)GetWRefCon( (WindowPtr)getXWindow() );
  32.  
  33.         GetPort( &oldXPort );                            // keep old port and...
  34.         SetPort( (WindowPtr)xWindow );                    // Setup new one.
  35.  
  36.         passMsg( true );    // the buck doesn't stop here
  37.  
  38.         }
  39.         
  40.     else {                    // plain jane call.
  41.  
  42.         xEventInfo    = nil;
  43.         xPrivStore    = nil;
  44.         oldXPort    = nil;
  45.         xWindow        = nil;
  46.         }
  47.  
  48. }
  49.  
  50. xcmdWindow::~xcmdWindow()
  51. {
  52.     if ( getXWindow() )        // We have a valid windowPtr so we should update
  53.         SetWRefCon( (WindowPtr)getXWindow(), (long)xPrivStore ); // We may have changed the handle.
  54.  
  55.     if ( isWindowEvent() )    // We were called through an "event" 
  56.         SetPort( oldXPort );                // restore old environment
  57. }
  58.  
  59. #pragma mark    -
  60. #pragma mark    • Make New WIndows…
  61.  
  62. CWindowPtr
  63. xcmdWindow::createWindow( short rID, ResType rType, Boolean flot )
  64. {
  65.     // get from a template.  (always a color window)
  66.     return xWindow = (CWindowPtr) 
  67.         GetNewXWindow( theParamPtr(), rType, rID, true, flot );
  68.     // don't do drawing here.  You'll get an event.
  69. }
  70.  
  71. CWindowPtr
  72. xcmdWindow::createWindow( Rect& r, xcmdString& title, short procID, Boolean vis, Boolean flot )
  73. {
  74.     // create from description.  (always a color window)
  75.     return xWindow = (CWindowPtr) 
  76.         NewXWindow( theParamPtr(), &r, StringPtr(title), vis, procID, true, flot );
  77.     // don't do drawing here.  You'll get an event.
  78. }
  79.  
  80. void
  81. xcmdWindow::idleTime( long ticks )
  82. {
  83.     // a subtle trick to determine if we have received the xOpenEvt.
  84.     // any call outside the scope of this object would have a null XWEventInfo*
  85.     if ( xEventInfo && xWindow )
  86.         SetXWIdleTime( theParamPtr(), (WindowPtr)xWindow, ticks );
  87. }
  88.  
  89. #pragma mark    -
  90. #pragma mark    • Event Bottleneck…
  91. void
  92. xcmdWindow::doWindowEvent( void )
  93. {
  94.     if ( !isWindowEvent() ) return;
  95.  
  96.     Point     p = xEvent.where;
  97.  
  98.     switch( xEvent.what ) {
  99.         case xOpenEvt:            /* the first event after you are created */
  100.                 doOpenWindow();
  101.                 break;
  102.         case xCloseEvt:            /* your window is being forced close (Quit?) */
  103.                 doCloseWindow();
  104.                 break;
  105.  
  106.         // We only get these calls if we made some special call to a call back.
  107.         case xGiveUpEditEvt:    /* you are losing Edit... */
  108.         case xEditUndo:            /* Edit——Undo */
  109.         case xEditCut:            /* Edit——Cut */
  110.         case xEditCopy:            /* Edit——Copy */
  111.         case xEditPaste:        /* Edit——Paste */
  112.         case xEditClear:        /* Edit——Clear */
  113.                 doEditEvent( xEvent.what );
  114.                 break;
  115.  
  116.         //case xGiveUpSoundEvt:    /* you are losing the sound channel... */
  117.                 // homey don't play this.
  118.  
  119.         case xMBarClickedEvt:    /* a menu is about to be shown--update if needed */
  120.                 doMenuUpdate();
  121.                 break;
  122.         case xMenuEvt:            /* user has selected an item in your menu */
  123.                 doMenuClick( long( getXEventParam(1) ), long( getXEventParam(2) ) );
  124.                 break;
  125.                 
  126.         // These are not documented well.
  127.         case xHidePalettesEvt:    /* someone called HideHCPalettes */
  128.         case xShowPalettesEvt:    /* someone called ShowHCPalettes */
  129.                 doShowHide( (xEvent.what==xShowPalettesEvt) );
  130.                 passMsg( false );
  131.                 break;
  132.  
  133.         case xSendEvt:            /* script has sent you a message (text) */
  134.                 doMessage( StringPtr( getXEventParam(0) ) );
  135.                 break;
  136.  
  137.         case xSetPropEvt:        /* set a window property */
  138.                 doSetProperty( StringPtr( getXEventParam(0) ), Handle( getXEventParam(1) ) );
  139.                 break;
  140.         case xGetPropEvt:        /* get a window property */
  141.                 xcmdString*    rc;
  142.                 rc = doGetProperty( StringPtr( getXEventParam(0) ) );
  143.                 setXEventResult( rc );
  144.                 delete rc;
  145.                 break;
  146.  
  147.         case xCursorWithin:        /* cursor is within the window */
  148.                 doCursorWithin();
  149.                 break;
  150.  
  151. // The standard toolbox events
  152.         case keyDown:
  153.         case autoKey:
  154.             doKeyDown( xEvent.message & charCodeMask, xEvent.modifiers );
  155.             break;
  156.  
  157.         case mouseDown:
  158.             WindowPtr w;
  159.             short    part    =     FindWindow(p, &w);
  160.             switch ( part ) {    // snatch mousedowns in buttons
  161.                 case inGrow:
  162.                     Rect r;
  163.                     SetRect(&r, 80, 80, 600, 380);
  164.                     long sze = GrowWindow(w, p, &r);
  165.                     ::InvalRect( &(w->portRect) );  // force a redraw.
  166.                     SizeWindow(w, LoWord(sze), HiWord(sze), true);
  167.                     passMsg( false );
  168.                     break;
  169.                 default:
  170.                     doMouseDown( part );
  171.             }
  172.  
  173.         case nullEvent:            /* only gets called if we have "interupt" code */
  174.                 doIdleStuff();
  175.                 break;
  176.  
  177.         case activateEvt:        /* window is bcoming active */
  178.                 doActiveWindow( xEvent.modifiers & activeFlag );
  179.                 break;
  180.  
  181.         case updateEvt:
  182.                 doUpdateWindow();
  183.                 passMsg( false );
  184.                 break;
  185.  
  186.         case app4Evt:            /* suspend/resume */
  187.                 doShowHide( (xEvent.message % 2 != 0) );
  188.                 passMsg( false );
  189.                 break;
  190.         } // switch
  191. }
  192.  
  193. #pragma mark    -
  194. #pragma mark    • Event Actions…
  195.  
  196. void
  197. xcmdWindow::doActiveWindow( Boolean activating )
  198. {    
  199.     // an activate/deactivate event.
  200.     if ( activating ) {
  201.         ; // do some activating
  202.     } else {
  203.         ; // do some deactivating
  204.     }
  205. }
  206.  
  207. void
  208. xcmdWindow::doShowHide( Boolean showFlag )
  209. {    // Handle events relating to app4Evt.  showFlag ? visible : invisible
  210.     ShowHide( (WindowPtr)getXWindow(), showFlag );
  211. }
  212.  
  213. void
  214. xcmdWindow::doUpdateWindow( void )
  215. {
  216.     ::BeginUpdate((WindowPtr)getXWindow());
  217.     
  218.         doDrawWindow();
  219.                 
  220.     ::EndUpdate((WindowPtr)getXWindow());
  221. }
  222.  
  223. // These are the default actions for the window. most do nothing.
  224. void
  225. xcmdWindow::doOpenWindow( int show )
  226. {
  227.     
  228.     // Window is openning, initialize
  229.     // Show it if it is hidden.
  230.     ShowHide( (WindowPtr)getXWindow(), show );
  231.     
  232. }
  233.  
  234. void
  235. xcmdWindow::doCloseWindow( void )
  236. {
  237.     // Window is closing, clean up
  238.     passMsg( true ); // go ahead, close it.
  239. }
  240.  
  241. void
  242. xcmdWindow::doEditEvent( short what )
  243. {
  244.     // Handle events relating to editing.
  245.     switch( what ) {
  246.             case xGiveUpEditEvt:    /* you are losing Edit... */
  247.             case xEditUndo:            /* Edit——Undo */
  248.             case xEditCut:            /* Edit——Cut */
  249.             case xEditCopy:            /* Edit——Copy */
  250.             case xEditPaste:        /* Edit——Paste */
  251.             case xEditClear:        /* Edit——Clear */
  252.             ;
  253.         }
  254. }
  255.  
  256. void
  257. xcmdWindow::doKeyDown( char keyChar, unsigned short mod )
  258. {
  259.     // We only get key down events if we register as a text editing environment
  260. }
  261.  
  262. void
  263. xcmdWindow::doMenuUpdate( void )
  264. {
  265.     // A menuBar click, set up menus
  266. }
  267.  
  268. void
  269. xcmdWindow::doMenuClick( long m, long i )
  270. {
  271.     // A menu click
  272. }
  273.  
  274.         
  275. void
  276. xcmdWindow::doMessage( const xcmdString& msg )
  277. {
  278.     // a send message to window...
  279. }
  280.  
  281. void
  282. xcmdWindow::doDrawWindow( void )
  283. {
  284.     // do some drawing.
  285. }
  286.         
  287. void
  288. xcmdWindow::doSetProperty( const xcmdString& prop, const xcmdString& val )
  289. {
  290.     // set a window property
  291.     // let hypercard handle it
  292.         passMsg( true );
  293. }
  294.  
  295. xcmdString*
  296. xcmdWindow::doGetProperty( const xcmdString& prop )
  297. {
  298.     // get a window property
  299.     // If you handle this property you create a new xcmdString* set its
  300.     //    value and return the pointer.  The caller will destroy it.
  301.     passMsg( true );    // let hypercard handle it
  302.     return nil;            // I didn't handle this!
  303. }
  304.  
  305. void
  306. xcmdWindow::doCursorWithin( void )
  307. {
  308.     // cursor is within the window */
  309.     passMsg( false );  // let hypercard handle it
  310. }
  311.  
  312. void
  313. xcmdWindow::doMouseDown( short part )
  314. {
  315.     
  316. }
  317.  
  318. void
  319. xcmdWindow::doIdleStuff( void )
  320. {
  321.     // This will only get called if we call idleTime with nonzero number.
  322. }
  323.  
  324. #pragma mark    -
  325. #pragma mark    • Accessors…
  326.  
  327.         // These functions must be used with great care.
  328.         // I don't check to see if the xEvent pointer is valid.
  329. CWindowPtr
  330. xcmdWindow::getXWindow( void )
  331. {
  332.     return xWindow;
  333. }
  334.  
  335. void*
  336. xcmdWindow::getXEventParam( int i )
  337. {
  338.     return (void*) xEventInfo->eventParams[i];
  339. }
  340.  
  341. void
  342. xcmdWindow::setXEventResult( xcmdString* rv )
  343. {
  344.     if ( rv ) 
  345.         xEventInfo->eventResult = Handle(*rv);
  346.     else
  347.         xEventInfo->eventResult = nil;  // nil
  348. }
  349.  
  350. EventRecord*
  351. xcmdWindow::getXEvent( void )
  352. {
  353.     return &xEvent;
  354. }
  355.  
  356. #pragma mark    -
  357. #pragma mark    • Protected…
  358.  
  359. void
  360. xcmdWindow::beginEdit( void )
  361. {
  362.     BeginXWEdit( theParamPtr(), (WindowPtr)xWindow );
  363. }
  364.  
  365. void
  366. xcmdWindow::endEdit( void )
  367. {
  368.     EndXWEdit( theParamPtr(), (WindowPtr)xWindow );
  369. }
  370.  
  371.  
  372. void
  373. xcmdWindow::allowReEntrancy( Boolean sysEvt, Boolean hcEvt )
  374. {
  375.     // a subtle trick to determine if we have received the xOpenEvt.
  376.     // any call outside the scope of this object would have a null XWEventInfo*
  377.     if ( xEventInfo && xWindow )
  378.         XWAllowReEntrancy( theParamPtr(), (WindowPtr)xWindow, sysEvt, hcEvt );
  379. }
  380.  
  381. void
  382. xcmdWindow::hasInteruptCode( Boolean hasIcode )
  383. {
  384.     // a subtle trick to determine if we have received the xOpenEvt.
  385.     // any call outside the scope of this object would have a null XWEventInfo*
  386.     if ( xEventInfo && xWindow )
  387.         XWHasInterruptCode( theParamPtr(), (WindowPtr)xWindow, hasIcode );
  388.     // suggestion, call "alwaysMoveHigh" right after creation of the window
  389.     //    that way, when you get the xOpenEvt you can lock the code and it will
  390.     //    be high in the stack.
  391. }
  392.  
  393. void
  394. xcmdWindow::alwaysMoveHigh( Boolean doIt )
  395. {
  396.     XWAlwaysMoveHigh( theParamPtr(), (WindowPtr)xWindow, doIt );
  397. }
  398.  
  399. void
  400. xcmdWindow::close( void )
  401. {
  402.     // close me (rarely used, let hc do it.)
  403.     if ( xWindow )
  404.         CloseXWindow( theParamPtr(), (WindowPtr)xWindow );
  405. }
  406.  
  407. short
  408. xcmdWindow::registerMenu( Boolean reg, short mResID )
  409. {
  410.     // give me a mResID and I'll create a unique id and hand it to you
  411.     if ( reg ) {
  412.         MenuRef mr = GetMenu( mResID );
  413.         if ( !mr ) return 0;  // invalid ID
  414.         // find a unique ID.
  415.         mResID = 1023;
  416.         MenuRef    menu;
  417.         do {
  418.             menu = GetMenuHandle( ++mResID );
  419.             } while ( menu );
  420.             
  421.         RegisterXWMenu( theParamPtr(), (WindowPtr)xWindow, mr, true );
  422.  
  423.         return mResID;
  424.         }
  425.         
  426.     // you are giving me a menuID.  I'll un-register the menu
  427.     else {
  428.         MenuRef    mr = GetMenuHandle( mResID );
  429.         RegisterXWMenu( theParamPtr(), (WindowPtr)xWindow, mr, false );
  430.         return 0;
  431.         }
  432. }
  433.